<?php


namespace App\Http\Controllers\Weapp;


use App\Model\SnappedSku;
use App\Tool\Json;
use Illuminate\Database\Eloquent\Builder;

class Snapped extends Base
{

    function snapped_list()
    {

        \DB::connection()->enableQueryLog();


        $list = SnappedSku::with('snapped')->with('goods')->with('goods_sku.goods_sku_detail')
            ->whereHas('snapped', function (Builder $query) {

                $query->where(function (Builder $query) {

                    $query->where('cycle', 1)->where('start_time', "<", time())
                        ->where('end_time', '>', time());

                })->orWhere(function (Builder $query) {

                    $query->where('cycle', 2)
                        ->where('every_day_start_time','<',strtotime("2020-1-23 ".date("H:i:s",time())))
                        ->where('every_day_end_time','>',strtotime("2020-1-23 ".date("H:i:s",time())));

                })->orWhere(function (Builder $query){

                    $week=(int)date("w",time());

                    if($week==0) $week=7;

                    $query->where('cycle', 3)->whereRaw("find_in_set(".$week.",week_time)");

                })->orWhere(function (Builder $query){

                    $day=date("j",time());

                    $query->where('cycle',4)->whereRaw("find_in_set(".$day.",date_time)");

                });

            })
            ->has('goods')->has('goods_sku')->orderBy('id', 'desc');

        $list = paginate($list, request()->input('p', 1));

        $logs = \DB::getQueryLog();

        return Json::code(1, $logs, $list);

    }


}
